﻿using Padsurface.Audio.Engine.Interfaces;
using System;
using System.Threading.Tasks;

namespace Padsurface.Audio.Engine
{
    internal class MetronomeRunningState : IMetronomeState
    {
        public bool CanStart
        {
            get { return true; }
        }

        public Task<bool> Start(Metronome context)
        {
            throw new InvalidOperationException();
        }

        public Task<bool> Stop(Metronome context)
        {
            if (context == null) throw new ArgumentNullException("context");

            context.SetState(new MetronomeStoppingState());

            context.StopCompletionTask = new TaskCompletionSource<bool>();
            context.CancellationTokenSource.Cancel();

            return context.StopCompletionTask.Task;
        }

        public void Reset(Metronome context)
        {
            if (context == null) throw new ArgumentNullException("context");

            context.Stopwatch.Restart();
        }
    }
}